#!/home/catmd5/.venv/bin/python3.8
import os
import socket
import sys

from lib.cmd_line import Server


def get_params():
    param = sys.argv
    if len(param) == 1:
        param = "few"
    elif len(param) == 2 and param[1] == '-a':
        param = "all"
    else:
        param = "help"
    return param


def update(ip, path):
    try:
        param = get_params()
        if param == "help":
            print(f"{ip}:命令错误，使用-a可更新配置参数")
            return
        socket.setdefaulttimeout(5)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ip, 22))
        s.send(bytes("INFO\r\n", 'UTF-8'))
        result = s.recv(1024).decode()
        line = Server(ip, 22, "muyi", "muyi@123")
        line.connect()
        line.open_channel()
        line.get_prompt(expect_symbol='$ ')
        mes = line.channel_send_cmd('docker ps -q --filter "name=mooe-core"', '$ ').split('\r\n')
        # path = os.path.dirname(os.path.abspath(__file__))
        # 普通车
        if len(mes) != 4:
            file_load_src = "/opt/ros/kinetic/lib/webcomm/"
            file_load_server = "/opt/ros/server/"
            tag = "mooe"
        else:
            tag = "mooe-core"
            file_load_src = "/home/muyi/"
            file_load_server = "/mooe/server/"
        line.open_sftp()
        line.sftp_put(f"{path}/src/clientport.py", f"{file_load_src}clientport.py")
        line.sftp_put(f"{path}/src/clientport_road.py", f"{file_load_src}clientport_road.py")
        line.sftp_put(f"{path}/src/config.py", f"{file_load_src}config.py")
        line.sftp_put(f"{path}/src/compute.py", f"{file_load_src}compute.py")
        line.sftp_put(f"{path}/src/server_bridge", f"{file_load_src}server_bridge")
        if param == "all":
            line.sftp_put(f"{path}/server/customer_id", f"{file_load_server}customer_id")
            line.sftp_put(f"{path}/server/networks.yaml", f"{file_load_server}networks.yaml")
            line.sftp_put(f"{path}/server/topic_list.yaml", f"{file_load_server}topic_list.yaml")
        if tag == "mooe":
            line.channel_send_cmd('echo "muyi@123" | sudo -S sudo chmod +777 /opt/ros/kinetic/lib/webcomm', '$ ')
            line.channel_send_cmd('cd /opt/ros/kinetic/lib/webcomm', '$ ')
        elif tag == "mooe-core":
            line.channel_send_cmd('cd /home/muyi', '$ ')
            line.channel_send_cmd('docker cp /mooe/server/topic_list.yaml mooe-core:/home/muyi/mooe-core/lib/webcomm',
                                  '$ ')
            line.channel_send_cmd('docker cp clientport.py mooe-core:/home/muyi/mooe-core/lib/webcomm', '$ ')
            line.channel_send_cmd('docker cp clientport_road.py mooe-core:/home/muyi/mooe-core/lib/webcomm', '$ ')
            line.channel_send_cmd('docker cp config.py mooe-core:/home/muyi/mooe-core/lib/webcomm', '$ ')
            line.channel_send_cmd('docker cp compute.py mooe-core:/home/muyi/mooe-core/lib/webcomm', '$ ')
            line.channel_send_cmd('docker cp server_bridge mooe-core:/home/muyi/mooe-core/lib/webcomm', '$ ')
            line.channel_send_cmd('docker exec -it mooe-core /bin/bash', '$ ')
            line.channel_send_cmd('cd /home/muyi/mooe-core/lib/webcomm', '$ ')
        cmd_kill_server = "ps -ef | grep -v grep | grep 'server_'  | awk '{print $2}' | xargs kill -9"
        line.channel_send_cmd(f'echo "muyi@123" | sudo -S sudo {cmd_kill_server}', '$ ')
        line.channel_send_cmd('python -m clientport.py', '$ ')
        line.channel_send_cmd('python -m clientport_road.py', '$ ')
        line.channel_send_cmd('python -m config.py', '$ ')
        line.channel_send_cmd(
            'chmod +111 clientport.pyc clientport.py clientport_road.pyc config.pyc compute.pyc server_bridge',
            '$ ')
        cmd_kill_clientport = "ps -ef | grep -v grep | grep 'clientport\.py'  | awk '{print $2}' | xargs kill -9 &&  \
                              ps -ef | grep -v grep | grep 'clientport_road\.py'  | awk '{print $2}' | xargs kill -9"
        line.channel_send_cmd(cmd_kill_clientport, '$ ')
        # line.channel_send_cmd("./clientport.pyc &", '$ ')
        print(f"{ip}:success")
    except:
        print(f"{ip}:error")
    finally:
        if line:
            line.close()
            del line
